<template>
  <view class="detail-container">
    <!-- 标题栏 -->
    <view class="nav-bar">
      <text class="back-icon" @click="goBack">＜</text>
      <text class="nav-title">订单详情</text>
      <view class="nav-placeholder"></view>
    </view>

    <!-- 订单信息卡片 -->
    <view class="detail-card">
      <!-- 核销码和车辆信息 -->
      <view class="section">
        <text class="label">核销码</text>
        <text class="value code">{{ order.verificationCode || '--' }}</text>
        <view class="row">
          <text class="label">车辆：</text>
          <text class="value">{{ (order.brand || '--') + '/' + (order.model || '--') + '/' + (order.plateNumber || '--') }}</text>
        </view>
      </view>
      <view class="divider"></view>
      <!-- 服务内容 -->
      <view class="section">
        <text class="label">服务内容</text>
        <view v-if="order.serviceNames && order.serviceNames.length" v-for="(name, idx) in order.serviceNames" :key="idx" class="row">
          <text class="service">{{ name }}</text>
        </view>
        <view v-else class="row">
          <text class="service">--</text>
        </view>
      </view>
      <view class="divider"></view>
      <!-- 订单详细信息 -->
      <view class="section">
        <text class="label">订单信息</text>
        <view class="row">
          <text>原价</text>
          <text>￥{{ order.totalPrice != null ? order.totalPrice : '--' }}</text>
        </view>
        <view class="row">
          <text>优惠</text>
          <text class="discount">-￥{{ order.discountPrice != null ? order.discountPrice : '--' }}</text>
        </view>
        <view class="row">
          <text>实付金额</text>
          <text class="actual">￥{{ order.payPrice != null ? order.payPrice : '--' }}</text>
        </view>
        <view class="row">
          <text>订单编号</text>
          <text>{{ order.orderNo || '--' }}</text>
        </view>
        <view class="row">
          <text>预约时间</text>
          <text>{{ order.reservationTime || '--' }}</text>
        </view>
        <view class="row">
          <text>支付时间</text>
          <text>{{ order.payTime || '--' }}</text>
        </view>
      </view>
    </view>

    <!-- 底部按钮 -->
    <view class="footer">
      <button
        class="confirm-btn"
        :class="{ disabled: isVerified }"
        :disabled="isVerified"
        @click="handleBtnClick"
      >
        {{ isVerified ? '已核销' : '确认核销' }}
      </button>
    </view>

    <!-- 弹窗 -->
    <view v-if="showDialog" class="dialog-mask" @click.self="showDialog = false">
      <view class="dialog-box">
        <view class="dialog-title">核销此订单</view>
        <view class="dialog-content">请您仔细核对订单，此操作不可撤销，请谨慎操作</view>
        <view class="dialog-actions">
          <button class="dialog-btn confirm" @click="handleVerify">核销</button>
          <button class="dialog-btn cancel" @click="showDialog = false">取消</button>
        </view>
      </view>
    </view>
  </view>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { onLoad } from '@dcloudio/uni-app'
import orderApi from '@/api/order'

const showDialog = ref(false)
const isVerified = ref(false)
const order = ref({})

const goBack = () => {
  uni.navigateBack();
}

const handleVerify = () => {
  showDialog.value = false
  isVerified.value = true
  uni.showToast({
    title: '核销成功',
    icon: 'success'
  })
}

const handleBtnClick = () => {
  if (isVerified.value) {
    uni.navigateBack()
  } else {
    showDialog.value = true
  }
}

// 获取详情
const fetchOrderDetail = async (orderId) => {
  try {
    const res = await orderApi.getOrderDetail(orderId)
    order.value = res.data || {}
  } catch (e) {
    order.value = {}
  }
}

// 兼容uniapp页面参数
onLoad((options) => {
  if (options.orderId) {
    fetchOrderDetail(options.orderId)
  }
})
</script>

<style scoped>
.detail-container {
  background: #f5f5f5;
  min-height: 100vh;
  padding-bottom: 120rpx;
}
.nav-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 100rpx;
  background: #fff;
  border-bottom: 1rpx solid #eee;
  padding: 0 24rpx;
  position: sticky;
  top: 0;
  z-index: 10;
}
.back-icon {
  font-size: 40rpx;
  color: #333;
  width: 60rpx;
  text-align: left;
}
.nav-title {
  font-size: 36rpx;
  font-weight: bold;
  color: #222;
}
.nav-placeholder {
  width: 60rpx;
}
.detail-card {
  background: #fff;
  border-radius: 16rpx;
  margin: 32rpx 24rpx 0 24rpx;
  box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.04);
  padding: 32rpx 24rpx 24rpx 24rpx;
}
.section {
  margin-bottom: 24rpx;
}
.label {
  font-size: 28rpx;
  color: #333;
  font-weight: bold;
  margin-bottom: 12rpx;
  display: block;
}
.value {
  font-size: 28rpx;
  color: #222;
}
.code {
  font-size: 30rpx;
  font-weight: bold;
  margin-left: 16rpx;
}
.row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 28rpx;
  margin: 8rpx 0;
}
.service {
  color: #333;
}
.service-price {
  color: #222;
}
.discount {
  color: #ff3b30;
}
.actual {
  color: #ff3b30;
  font-weight: bold;
}
.divider {
  height: 1rpx;
  background: #e5e5e5;
  margin: 0 -24rpx 24rpx -24rpx;
}
.footer {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  background: #fff;
  padding: 24rpx 0 32rpx 0;
  display: flex;
  justify-content: center;
  box-shadow: 0 -2rpx 8rpx rgba(0,0,0,0.04);
}
.confirm-btn {
  background: #2979ff;
  color: #fff;
  border: none;
  border-radius: 12rpx;
  font-size: 32rpx;
  padding: 18rpx 0;
  width: 80%;
  font-weight: bold;
  transition: background 0.2s;
}
.confirm-btn.disabled {
  background: #cccccc;
  color: #fff;
}
.dialog-mask {
  position: fixed;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  background: rgba(0,0,0,0.3);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}
.dialog-box {
  background: #fff;
  border-radius: 24rpx;
  width: 80vw;
  max-width: 600rpx;
  padding: 48rpx 32rpx 32rpx 32rpx;
  box-shadow: 0 8rpx 32rpx rgba(0,0,0,0.08);
  display: flex;
  flex-direction: column;
  align-items: center;
}
.dialog-title {
  font-size: 32rpx;
  font-weight: bold;
  margin-bottom: 24rpx;
  color: #222;
}
.dialog-content {
  font-size: 28rpx;
  color: #666;
  margin-bottom: 40rpx;
  text-align: center;
}
.dialog-actions {
  display: flex;
  width: 100%;
  justify-content: space-between;
  gap: 32rpx;
}
.dialog-btn {
  flex: 1;
  font-size: 28rpx;
  border-radius: 12rpx;
  padding: 16rpx 0;
  border: none;
}
.dialog-btn.confirm {
  background: #2979ff;
  color: #fff;
  font-weight: bold;
}
.dialog-btn.cancel {
  background: #f5f5f5;
  color: #333;
}
</style>
